<template>
  <div class="page">
    <PageHeader></PageHeader>
    <div class="form">
      <div class="form-tile">{{ $t('userLogin') }}</div>
      <div class="form-input">
        <span>用户名</span>
        <input
          :class="usernameErr ? 'input-err' : ''"
          type="text"
          v-model="userForm.username"
          placeholder="请输入您的用户名"
        />
      </div>
      <div class="form-input">
        <span>密码</span>
        <input
          :class="passwordErr ? 'input-err' : ''"
          type="password"
          v-model="userForm.password"
          placeholder="请输入您的密码"
        />
      </div>
      <!-- <div class="form-text-btn">
        <div class="text-btn">没有账号？去注册</div>
      </div> -->
      <div class="form-btn">
        <div class="primary-btn" @click="login">登录</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import PageHeader from '@/components/PageHeader.vue'
import request from '@/utils/request.js'
import { setToken } from '@/utils/token.js'
import { useRouter } from 'vue-router'

const usernameErr = ref(false)
const passwordErr = ref(false)
const userForm = reactive({
  username: '',
  password: ''
})
const router = useRouter()

async function login() {
  const url = '/v1/users/login'
  const { data: res } = await request.post(url, userForm)
  setToken(res.data)
  router.push('/account')
}
</script>

<style lang="scss" scoped>

.form {
  padding: 12px;
  .form-tile {
    font-size: 22px;
    line-height: 1;
    padding: 26px 10px 24px;
  }
  .form-input {
    padding: 0 6px 24px;
    position: relative;
    span {
      height: 48px;
      line-height: 48px;
      font-size: 16px;
      position: absolute;
      left: 12px;
    }
    input {
      height: 48px;
      font-size: 16px;
      width: 100%;
      border: none;
      border-bottom: 1px solid rgba(204, 204, 204, 0.6);
      padding-left: 98px;
      padding-right: 8px;
    }
    .input-err {
      border-bottom-color: rgba(241, 36, 36, 1);
    }
  }
  .form-btn {
    width: 100%;
    padding-top: calc(100vh - 66px - 24px - 70px - 48px - 72px * 2);
    .primary-btn {
      margin: 12px auto;
      background-color: #1dbaf3;
      color: white;
      height: 46px;
      line-height: 46px;
      width: 58vw;
      font-size: 16px;
      text-align: center;
      border-radius: 6px;
      user-select: none;
      &:active {
        opacity: 0.8;
      }
    }
  }
}

.form-text-btn {
  padding: 6px 12px 24px;
  .text-btn {
    color: #666;
    font-size: 14px;
    line-height: 1;
    &:active {
      opacity: 0.8;
      text-decoration: underline;
    }
  }
}

.form-btn {
  padding-top: calc(
    100vh - 66px - 24px - 70px - 48px - 72px * 3 - 44px
  ) !important;
}
</style>
